package com.huo.base.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.channels.FileChannel;

/**
 * 流 相关的
 * http://localhost:10061/io/inputStream
 */
@Controller
@RequestMapping("/io")
public class IoController {

    /**
     * 字节流
     * @param request
     * @param response
     * @return
     * @throws FileNotFoundException
     */
    @GetMapping(value = "/inputStream")
    public String inputStream(HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException {
        byte [] bytes =  new byte[1024];
        FileInputStream inputStream = new FileInputStream(  "G:\\test.txt");//把文件放入到输入流对象中
        try {
            int len = 0;
            //inputStream.read( bytes )//从输入流读取bytest.length的字节，并储存到缓冲区bytes数组中
            while((len = inputStream.read( bytes )) != -1){
                inputStream.available();//从此输入流获取的剩余字节数
                System.out.println(inputStream.available());
            }
            System.out.println("================================進入");

            inputStream.close();//关闭此文件输入流，并释放和此流有关的所有系统资源
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "frame";
    }


    /**
     * 文件复制（），FileChannel 直接连接文件输入输出流的文件通道，将数据直接写入到目标文件中去
     * 效率高于流
     * https://www.cnblogs.com/yjd_hycf_space/p/7717703.html
     * @param request
     * @param response
     * @throws FileNotFoundException
     */
    @GetMapping("/fileChannel")
    public void fileChannel(HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException {
        System.out.println(File.separator);// \
        //文件路径
        File sourceFile = new File( "G:"+File.separator +"test.txt" );
        File targerFile = new File( "G:"+File.separator+"test3.txt" );
        //输入输出流
        FileInputStream inputStream = new FileInputStream( sourceFile );
        FileOutputStream outputStream = new FileOutputStream( targerFile  );
        //文件通道  NIO
        FileChannel inputChannel = inputStream.getChannel();
        FileChannel outputChannel = outputStream.getChannel();

        try {
            System.out.println(inputChannel.size());
            inputChannel.transferTo( 0,inputChannel.size(),outputChannel );
            inputChannel.force( true );//强制将内存中的数据 刷新进硬盘
            inputStream.close();
            inputChannel.close();
            outputStream.close();
            outputChannel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    @GetMapping(value = "/outputStream")
    public String outputStream(HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException {

        byte [] bytes =  new byte[10];
        FileInputStream inputStream = new FileInputStream(  "G:\\test.txt");//把文件放入到输入流对象中
        try {
             int len = 0;
            //int byteStream =  inputStream.read( bytes );
           ServletOutputStream outputStream =  response.getOutputStream();
            while ((len = inputStream.read( bytes )) == -1){
                outputStream.write( bytes,0,len );
        }
            outputStream.flush();
            outputStream.close();
            inputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return "frame";
    }

    /**
     * BufferedReader 为输入流提供缓冲区
     *
     * http://localhost:10061/io/bufferedReader
     * @param request
     * @param response
     * @throws FileNotFoundException
     */
    @GetMapping(value = "/bufferedReader")
    public void bufferedReader(HttpServletRequest request, HttpServletResponse response) throws IOException {
        FileReader fileReader = new FileReader(  "G:\\test.txt");
        BufferedReader reader = new BufferedReader(fileReader,8*1024);

        FileWriter fileWriter = new FileWriter( "G:\\testd.txt" );
        //创建输出流缓冲区
        BufferedWriter bufferedWriter = new BufferedWriter( fileWriter );//为输出流提供缓冲区
        String str = null;
        //读完为止
        while ((str = reader.readLine())!=null){
            System.out.println(" 每次读取文件中一整行的字符:"+str);//readLine 每次读取文件中一整行的字符
            bufferedWriter.write( str );//把输入流缓冲区中的字符，写入到指定路径文件中
            bufferedWriter.write( "\r\n" );//自动换行
            //bufferedWriter.newLine();//自动换行
        }
        bufferedWriter.flush();
        reader.close();
        fileReader.close();
        bufferedWriter.close();
    }


    @GetMapping(value = "/bufferedReader3")
    public void bufferedReade3r(HttpServletRequest request, HttpServletResponse response) throws IOException {
        InputStream inputStream = request.getInputStream();//字节流



    }


}
